﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using A2DFramework.InstanceService;

namespace A2DFramework.ValidationService
{
    public static class ValidationServiceHelper
    {
        public static List<BrokenRule> IsValid<T>(this T o, string scene = "", bool only1Level = false)
        {
            List<BrokenRule> errors = new List<BrokenRule>();

            List<IValidatorExecutor> validatorExecutors = GetValidatorExecutors();

            foreach (var validator in validatorExecutors)
            {
                List<BrokenRule> tmpErrors = validator.IsValid<T>(o, scene, only1Level);
                errors.AddRange(tmpErrors);
            }

            return errors;
        }

        private static List<IValidatorExecutor> GetValidatorExecutors()
        {
            List<IValidatorExecutor> validatorExecutors = new List<IValidatorExecutor>();
            validatorExecutors.Add(new ValidatorExecutor());

            if (ObjectFactory.IsRegistered<IValidatorExecutor>())
            {
                IValidatorExecutor extendedExecutor = ObjectFactory.Resolve<IValidatorExecutor>();
                validatorExecutors.Add(extendedExecutor);
            }
            return validatorExecutors;
        }
    }
}